package com.gxa.learning.portal.service.impl;

import com.gxa.learning.core.common.ExceptionCode;
import com.gxa.learning.core.exception.BusinessException;
import com.gxa.learning.core.pojo.User;
import com.gxa.learning.core.service.impl.BaseServiceImpl;
import com.gxa.learning.portal.mapper.FeedbackMapper;
import com.gxa.learning.portal.mapper.MessageMapper;
import com.gxa.learning.portal.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lijianmin
 * @date 2021/12/09  15:59:36
 */
@Service("messageService")
public class MessageServiceImpl extends BaseServiceImpl<User> implements MessageService {
    public static final String TESTKEY = "wodekaoshi";

    public static final String LOGIN = "#Zhao-Yang#=Mi+Cai=";

    @Resource
    private MessageMapper messageMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private FeedbackMapper feedbackMapper;

    @Override
    public List<Map<String, Object>> selectUserTest(Long id, Long projectId) throws ParseException {
        List<Map<String, Object>> maps = messageMapper.selectUserTest(id, projectId);
        for (Map<String, Object> map : maps) {
            Object scoreKey = map.get("scoreKey");
            Object o = redisTemplate.opsForValue().get(TESTKEY + scoreKey);
            if (o != null) {
                Map<String, Object> entries = redisTemplate.opsForHash().entries(scoreKey);
                map.putAll(entries);
            }
            Object endDays = map.get("endDay");
            if (endDays != null) {
                String time = map.get("time").toString();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                long endDay = Long.parseLong(endDays.toString());
                Date parse = sdf.parse(time);
                map.put("time", parse);
                long outTime = sdf.parse(time).getTime() + (endDay * 24 * 60 * 60 * 1000);
                long presentTime = System.currentTimeMillis();
                if (presentTime > outTime) {
                    messageMapper.update(Long.parseLong(map.get("scoreId").toString()));
                    map.put("status", 2);
                }
                Date date = new Date(outTime);
                map.put("endDay", sdf.format(date));
            }
        }
        return maps;
    }

    @Override
    public List<Map<String, Object>> selectUserFeedback(Long id) throws BusinessException {
        if (id == null) {
            throw new BusinessException(ExceptionCode.ARGUMENTS_NOT_NULL);
        }
        return feedbackMapper.selectUserFeedback(id);
    }

    @Override
    public void logOut(String phone) throws BusinessException {

        User user = new User();
        user.setPhone(phone);
        List<User> select = messageMapper.select(user);
        if (select.size() == 0) {
            throw new BusinessException(ExceptionCode.NOT_TRUE_PHONE);
        }
        User user1 = select.get(0);
        //获取数据库存储的token，因为redis存放的token的键是用数据库的token+LOGIN组成的
        String token = user1.getToken();

        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        //如果token没过期，那就删除token
        Object o = ops.get(LOGIN + token);
        if (o == null) {
            throw new BusinessException(ExceptionCode.ERORR_LOGIN);
        }
        redisTemplate.delete(LOGIN + token);
    }
}
